package com.example.vietvoicecmdsimple;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.AlarmClock;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import android.app.Activity;

public class ExecuteCommand
{
	private static final int MEDIA_TYPE_IMAGE = 0;
	private static final int MEDIA_TYPE_VIDEO = 0;
	public static Intent intent;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}
	

	public void ExecuteCommand(Context context, Activity activity)
	{
		Toast.makeText(context, "Hahaha", Toast.LENGTH_LONG).show();
		
		int index = 3;
		String parameter1 = "01667250833";
		String parameter2 = "Hello Microsoft, cau mong up len duoc dum cong. Day la san pham dau tien cua con va vo. Xin ngai";
		String parameter3 = "";
		
		int hour = 1;
		int minute = 30;
		
		String name = "Luan";
		String phone = "01667250833";
		String email = "noblakit01@gmail.com";
		
		String appname = "Camera";
		
		String web = "www.google.com";
		switch(index)
		{
			case 0:
				intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + parameter1));
				activity.startActivity(intent);
				break;
			
			case 1:
				intent = new Intent(Intent.ACTION_VIEW);
				intent.putExtra("address", parameter1);
				intent.putExtra("sms_body", parameter2);
				intent.setType("vnd.android-dir/mms-sms");
				activity.startActivity(intent);
				break;
			case 2:
				intent = new Intent(AlarmClock.ACTION_SET_ALARM);
				intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
				intent.putExtra(AlarmClock.EXTRA_MINUTES, minute);
				activity.startActivity(intent);
				break;
			case 3:
				intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
			    
				intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
			    intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
			    intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
			    //intent.putExtra(ContactsContract.Contacts.DISPLAY_NAME, name);

			    activity.startActivity(intent);
			    break;
			case 4:
				if (appname.equals("Camera"))
				{
					intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
					activity.startActivity(intent);
				}
				else if (appname.equals("Contact"))
				{
					try
					{
						intent = new Intent(Intent.ACTION_VIEW);
						intent.setData(Uri.parse("content://contacts/people/"));
						activity.startActivity(intent);
					} 
					catch (Exception e)
					{	
					}
				}
				else if (appname.equals("Image"))
				{
					try {
						intent = new Intent(Intent.ACTION_GET_CONTENT);
						intent.setType("image/*");
						activity.startActivity(intent);
					}
					catch (Exception e){}
				}
				else if (appname.equals("Calendar"))
				{
					try 
					{
						Calendar today = Calendar.getInstance();
						
			            Uri uriCalendar = Uri.parse("content://com.android.calendar/time/" + String.valueOf(today.getTimeInMillis()));
			            intent = new Intent(Intent.ACTION_VIEW,uriCalendar);	            
			            activity.startActivity(intent);
					}
					catch (Exception e){}
				}
				else if (appname.equals("Music"))
				{
					try 
					{
						intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
						
		                activity.startActivity(intent);
					}
					catch (Exception e){}
				}
				break;
			case 5:
				try
				{
					intent = new Intent(Intent.ACTION_VIEW, Uri.parse(web));
					activity.startActivity(intent);
				}
				catch (Exception e){}
				break;
		}
		Toast.makeText(context, "Hihiha", Toast.LENGTH_LONG).show();
		
	}
	
	private static Uri getOutputMediaFileUri(int type){
	      return Uri.fromFile(getOutputMediaFile(type));
	}
	
	private static File getOutputMediaFile(int type){
	    // To be safe, you should check that the SDCard is mounted
	    // using Environment.getExternalStorageState() before doing this.

	    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
	              Environment.DIRECTORY_PICTURES), "MyCameraApp");
	    // This location works best if you want the created images to be shared
	    // between applications and persist after your app has been uninstalled.

	    // Create the storage directory if it does not exist
	    if (! mediaStorageDir.exists()){
	        if (! mediaStorageDir.mkdirs()){
	            Log.d("MyCameraApp", "failed to create directory");
	            return null;
	        }
	    }

	    // Create a media file name
	    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(10));
	    File mediaFile;
	    if (type == MEDIA_TYPE_IMAGE){
	        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
	        "IMG_"+ timeStamp + ".jpg");
	    } else if(type == MEDIA_TYPE_VIDEO) {
	        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
	        "VID_"+ timeStamp + ".mp4");
	    } else {
	        return null;
	    }

	    return mediaFile;
	}
}
